<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{

    private $_id;
    public $email;
    public $_name;
    public $authToken;

    const ERROR_USER_NOT_ACTIVE = 13;

    public function __construct($email, $password = null, $authToken = "")
    {
        parent::__construct($email, $password);
        $this->email     = $email;
        $this->password  = $password;
        $this->authToken = $authToken;
    }

    public function init()
    {
        Yii::app()->user->setStateKeyPrefix('_admin');
    }

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        $role = isset($_POST['role']) ? $_POST['role'] : Users::TYPE_CUSTOMER;

        if (!empty($this->authToken))
        {
            $appUserSess = AppUserSession::model()->findByAttributes(array('authToken' => $this->authToken));
            if ($appUserSess !== NULL)
            {
                $user = Users::model()->find('LOWER(email)=:email AND type=:type', array(
                    ':email' => strtolower($appUserSess->user),
                    ':type'  => $role
                ));

                if ($user === null)
                    $this->errorCode = self::ERROR_USERNAME_INVALID;
                elseif ($user->status == Users::STATUS_BLOCK)
                {
                    $this->errorCode = self::ERROR_USER_NOT_ACTIVE;
                }
                else
                {
                    $this->_id       = $user->id;
                    $this->email     = $user->email;
                    $this->_name     = $user->name;
                    $this->errorCode = self::ERROR_NONE;
                }
            }
        }
        else
        {
            $user            = Users::model()->find('LOWER(email)=:email AND type=:type', array(
                ':email' => strtolower($this->email),
                ':type'  => $role
            ));
            if ($user === null)
                $this->errorCode = self::ERROR_USERNAME_INVALID;

            else if (!$user->validatePassword($this->password))
            {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            }
            elseif ($user->status == Users::STATUS_BLOCK)
            {
                $this->errorCode = self::ERROR_USER_NOT_ACTIVE;
            }
            else
            {
                $this->_id       = $user->id;
                $this->email     = $user->email;
                $this->_name     = $user->name;
                $this->errorCode = self::ERROR_NONE;
            }
        }

        return $this->errorCode;
    }

    public function authenticateFB()
    {
        $user            = Users::model()->find('LOWER(email)=?', array(strtolower($this->email)));
        if ($user === null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        elseif ($user->status !== Users::STATUS_ACTIVE)
        {
            $this->errorCode = self::ERROR_USER_NOT_ACTIVE;
        }
        else
        {
            $this->_id       = $user->id;
            $this->email     = $user->email;
            $this->_name     = $user->name;
            $this->errorCode = self::ERROR_NONE;
        }
        return $this->errorCode;
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId()
    {
        return $this->_id;
    }

    public function getName()
    {
        return $this->_name;
    }

}
